home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / CodeWarrior Lite / Metrowerks C⁄C++ Lite / Libraries / Runtime / Runtime PPC / Sources / mwstdcsetjmp.s < prev    next >
Encoding:
Text File  |  1995-01-24  |  3.7 KB  |  170 lines  |  [TEXT/MPS ]

  1. #    mwstdcsetjmp.s    -    setjmp() and longjmp() routines for Metrowerks C++ for PowerPC
  2. #
  3. #    Copyright © 1993 metrowerks inc.  All Rights Reserved.
  4. #
  5. #
  6. #    THEORY OF OPERATION
  7. #
  8. #    The runtime support routines __setjmp() and longjmp() support the C <setjmp.h>
  9. #    facilities.
  10. #
  11. #    __setjmp() captures the state of the program in a jmp_buf data structure
  12. #    which has the following C definition:
  13. #
  14. #        typedef struct jmp_buf {
  15. #            unsigned long    PC;            //    0: saved PC
  16. #            unsigned long    CR;            //    4: saved CR
  17. #            unsigned long    SP;            //  8: saved SP
  18. #            unsigned long    RTOC;        // 12: saved RTOC
  19. #            unsigned long    ldc;        // 16: saved __local_destructor_chain
  20. #            unsigned long    GPRs[19];    // 20: saved r13-r31
  21. #            double            FPRs[18];    // 96: saved fp14-fp31
  22. #            double            FPSCR;        //240: saved FPSCR
  23. #        } *jmp_buf;
  24. #
  25. #    longjmp() restores the state, effecting a transfer back to the saved PC with
  26. #    appropriate registers, stack, TOC, etc.
  27. #
  28. #    In <setjmp.h> the jmp_buf type must be defined as an array (per ANSI rules):
  29. #
  30. #        typedef long jmp_buf[63];
  31. #
  32. #    setjmp() and longjmp() are defined as follows:
  33. #
  34. #        int __setjmp(jmp_buf env);
  35. #        #define setjmp(env) __setjmp(env)
  36. #        void longjmp(jmp_buf env, int val);
  37. #
  38. #
  39. #    BUILD INSTRUCTIONS
  40. #
  41. #    To assemble this file:
  42. #
  43. #        ppcasm mwstdcsetjmp.s -o mwstdcsetjmp.o
  44. #
  45. #    The object file runtime.o can be added directly to any CodeWarrior™ project.
  46. #
  47.  
  48.         dialect    powerpc
  49.  
  50. #
  51. #    Assembler Equates
  52. #
  53. cr0        equ        0
  54. cr1        equ        1
  55. cr2        equ        2
  56. cr3        equ        3
  57. cr4        equ        4
  58. cr5        equ        5
  59. cr6        equ        6
  60. cr7        equ        7
  61.  
  62. #
  63. #    Extern Data
  64. #
  65.         import    __local_destructor_chain{RW}
  66.  
  67. #
  68. #    Public Data
  69. #
  70.         csect    __setjmp{DS}
  71.         export    __setjmp{DS}
  72.         dc.l    .__setjmp{PR}
  73.         dc.l    TOC{TC0}
  74.         
  75.         csect    longjmp{DS}
  76.         export    longjmp{DS}
  77.         dc.l    .longjmp{PR}
  78.         dc.l    TOC{TC0}
  79.  
  80. #
  81. #    TOC pointers
  82. #
  83.         toc
  84.         tc        __local_destructor_chain{TC}, __local_destructor_chain{RW}
  85.  
  86.  
  87. #    __setjmp    -    C setjmp() routine
  88. #
  89. #    On entry R3 points to a jmp_buf struct. On exit, R3 is 0.
  90. #
  91.         csect    .__setjmp{PR}
  92.         export    .__setjmp{PR}
  93.         mflr    r5
  94.         mfcr    r6
  95.         stw        r5,0(r3)        #    save PC (LR)
  96.         stw        r6,4(r3)        #    save CR
  97.         stw        SP,8(r3)        #    save SP
  98.         stw        RTOC,12(r3)        #    save RTOC
  99.         stmw    r13,20(r3)        #    save r13-r31
  100.         mffs    fp0
  101.         stfd    fp14,96(r3)        #    save fp14-fp31
  102.         stfd    fp15,104(r3)
  103.         stfd    fp16,112(r3)
  104.         stfd    fp17,120(r3)
  105.         stfd    fp18,128(r3)
  106.         stfd    fp19,136(r3)
  107.         stfd    fp20,144(r3)
  108.         stfd    fp21,152(r3)
  109.         stfd    fp22,160(r3)
  110.         stfd    fp23,168(r3)
  111.         stfd    fp24,176(r3)
  112.         stfd    fp25,184(r3)
  113.         stfd    fp26,192(r3)
  114.         stfd    fp27,200(r3)
  115.         stfd    fp28,208(r3)
  116.         lwz        r5,__local_destructor_chain{TC}(RTOC)
  117.         stfd    fp29,216(r3)
  118.         stfd    fp30,224(r3)
  119.         lwz        r5,0(r5)
  120.         stfd    fp31,232(r3)
  121.         stfd    fp0,240(r3)        #    save FPSCR
  122.         stw        r5,16(r3)        #    save __local_destructor_chain
  123.         li        r3,0
  124.         blr
  125.  
  126.  
  127. #    longjmp        -    C longjmp() routine
  128. #
  129. #    On entry R3 points to a jmp_buf struct and R4 contains the return value.
  130. #    On exit, R3 contains 1 if R4 was 0, otherwise it contains the value from R4.
  131. #
  132.         csect    .longjmp{PR}
  133.         export    .longjmp{PR}
  134.         lwz        r5,0(r3)
  135.         lwz        r6,4(r3)
  136.         mtlr    r5                #    restore PC (LR)
  137.         mtcrf    255,r6            #    restore CR
  138.         lwz        SP,8(r3)        #    restore SP
  139. ###        lwz        RTOC,12(r3)        #    don't restore RTOC yet (used below)
  140.         lmw        r13,20(r3)        #    restore r13-r31
  141.         lfd        fp14,96(r3)        #    restore fp14-fp31
  142.         lfd        fp15,104(r3)
  143.         lfd        fp16,112(r3)
  144.         lfd        fp17,120(r3)
  145.         lfd        fp18,128(r3)
  146.         lfd        fp19,136(r3)
  147.         lfd        fp20,144(r3)
  148.         lfd        fp21,152(r3)
  149.         lfd        fp22,160(r3)
  150.         lfd        fp23,168(r3)
  151.         lfd        fp24,176(r3)
  152.         lfd        fp25,184(r3)
  153.         lfd        fp26,192(r3)
  154.         lfd        fp27,200(r3)
  155.         lfd        fp28,208(r3)
  156.         lwz        r5,16(r3)        #    restore __local_destructor_chain
  157.         lfd        fp29,216(r3)
  158.         lfd        fp30,224(r3)
  159.         lwz        r6,__local_destructor_chain{TC}(RTOC)
  160.         lfd        fp0,240(r3)
  161.         lfd        fp31,232(r3)
  162.         stw        r5,0(r6)
  163.         lwz        RTOC,12(r3)        #    restore RTOC now!
  164.         cmpwi    r4,0
  165.         mr        r3,r4
  166.         mtfsf    255,fp0            #    restore FPSCR
  167.         bnelr                    #    return value in R4
  168.         li        r3,1            #    return 1
  169.         blr
  170.